from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.views import APIView
from rest_framework import mixins
from rest_framework import viewsets

from goods.models import Goods, GoodsCategory
from .serializers import GoodsSerializer,CategorySerializer
from .filter import GoodsFilter

# Create your views here.


class GoodsPagination(PageNumberPagination):
    """自定义分页器类"""
    page_size = 12
    page_size_query_param = 'page_size'
    page_query_param = 'page'



class GoodsListViewSet(mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet):
    """
    list:
        商品列表，分页，搜索，过滤，排序
    retrieve:
        获取商品详情
    """
    # 指定queryset
    queryset = Goods.objects.all().order_by('id')
    # 指定序列化器
    serializer_class = GoodsSerializer
    # 设置filter的类为我们自定义的guo类
    filter_class = GoodsFilter
    # 自定义分页器类
    pagination_class = GoodsPagination
    # 指定排序字段,销量、价格
    ordering_fields = ('sold_num','shop_price')
    # 指定搜索字段
    search_fields = ('name', 'goods_brief', 'goods_desc')



class CategoryViewSet(mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet):
    """
     商品分类列表数据
    """
    # 指定queryset
    queryset = GoodsCategory.objects.filter(category_type=1).all()
    # 指定序列化器
    serializer_class = CategorySerializer

